package com.alphaking.Jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alphaking.bean.Share;
import com.alphaking.bean.Upvote;
import com.mysql.jdbc.PreparedStatement;

public class JdbcCollections {

	private static Connection con = JdbcUtils.getCon();

	/**
	 * 查看收藏
	 * 
	 * @param account
	 * @return
	 */
	public static List<Share> getCollection(String account) {

		List<Share> listShares = new ArrayList<Share>();
		String sqlSearch = "select * from share where shareid in " + "(select collect_shareid from collection "
				+ "where user_account = ?)";

		try {

			PreparedStatement pstmt = (PreparedStatement) con.prepareStatement(sqlSearch);

			pstmt.setString(1, account);

			ResultSet rst = pstmt.executeQuery();

			while (rst.next()) {
				Share share = new Share();
				share.setShareId(rst.getInt("shareid"));
				share.setShareContent(rst.getString("sharecontent"));
				String[] images = rst.getString("imageaddress").split(",");
				List<String> imageadress = new ArrayList<String>();
				for (int i = 0; i < images.length; i++) {
					imageadress.add(images[i]);
				}
				share.setImageAddress(imageadress);
				share.setNumberOfcomments(rst.getInt("numcomments"));
				share.setNumberOfupvotes(rst.getInt("numUpvotes"));
				share.setComments(JdbcComments.getComments(rst.getInt("shareid")));
				List<Upvote> upvotes = JdbcUpvote.getUpvote(share.getShareId());
				share.setUpvotes(upvotes);
				share.setShareDate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(rst.getDate("sharedate").toString()));
				listShares.add(share);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return listShares;

	}

}
